package com.link510.aitools.activity.airobot.core;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.domain.device.DeviceInfo;
import com.link510.aitools.core.helper.TypeHelper;
import com.link510.aitools.core.helper.ValidateHelper;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.Airobots;
import com.link510.aitools.services.ApplicationUtils;
import com.link510.aitools.services.CustomWaitDialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class RobotCoreEditIPActivity extends BaseAcitivity {

    /**
     * 加载
     */
    private CustomWaitDialogUtils customWaitDialogUtils;

    @BindView(R.id.ip_edit_text)
    EditText ipEditText;

    @BindView(R.id.port_edit_text)
    EditText portEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot_core_edit_ip);
        customWaitDialogUtils = new CustomWaitDialogUtils(this);
        initData();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {

        /*
         * 设备信息
         */
        DeviceInfo deviceInfo = ApplicationUtils.getInstance().getApplication().getDeviceInfo();

        if (deviceInfo == null || Strings.isNullOrEmpty(deviceInfo.getDeviceSN())) {
            ToastUtils.showShort("设备读取异常");
            finish();
            return;
        }

        ipEditText.setText("114.55.252.81");
        portEditText.setText("8060");
    }

    /**
     * 设备名称修改
     */
    @OnClick(R.id.send_button)
    public void sendButtonClick() {

        try {

            String ip = ipEditText.getText().toString().trim();

            if (Strings.isNullOrEmpty(ip) || !ValidateHelper.getInstance().isIP(ip)) {
                ToastUtils.showShort("IP地址不正确");
                return;
            }

            int port = TypeHelper.getInstance().stringToInt(portEditText.getText().toString().trim(), 0);

            if (port <= 1024 || port >= 65535) {
                ToastUtils.showShort("请输入正确的端口");
                return;
            }

            customWaitDialogUtils.show("设备名称修改中...");
            Airobots.getInstance().editIpAndPort(this.deviceSN, ip, port, new CWMListener<MessageInfo<String>>() {
                @Override
                public void onSuccess(String message, MessageInfo<String> stringMessageInfo) {

                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备IP修改成功");
                }

                @Override
                public void onError(int code, String message) {

                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备IP修改失败");
                }
            });


        } catch (Exception ignored) {

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        customWaitDialogUtils.dismiss();
    }

}